// ************************** Motor_L.c **********************************

#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/timer.h"
#include "driverlib/gpio.h"
#include "driverlib/pwm.h"
#include "driverlib/sysctl.h"
#include "lm3s8962.h"

//*Timer
#define PWM_TIMER_BASE_A            TIMER0_BASE           //
#define PWM_TIMER_BASE_B            TIMER1_BASE
#define PWM_TIMER_PERIPH_ENABLE_A   SYSCTL_PERIPH_TIMER0  //
#define PWM_TIMER_PERIPH_ENABLE_B   SYSCTL_PERIPH_TIMER1  //
#define PWM_PORT_BASE_A             GPIO_PORTB_BASE
#define PWM_PORT_BASE_B             GPIO_PORTB_BASE
#define PWM_PORT_PIN_A              GPIO_PIN_0
#define PWM_PORT_PIN_B              GPIO_PIN_1
#define PWM_SYSCTL_PORT_A           SYSCTL_PERIPH_GPIOB
#define PWM_SYSCTL_PORT_B           SYSCTL_PERIPH_GPIOB
#define PWM_PORT_PIN_CONFIG_A       GPIO_PB0_PWM2
#define PWM_PORT_PIN_CONFIG_B       GPIO_PB1_PWM3
//*Other
#define TRUE                            1
#define FALSE                           0
#define CLOCK_L                         2500000

// **********************************************************************
// ************************** GLOBALS *********************************
// ******************************************************************
/*static*/ volatile unsigned long PWM_Period_L;
// ******************************************************************
// ***********************************************************************

// 
void Motor_L_DutyCycle( unsigned long dutyCycle_A, unsigned long dutyCycle_B ){
  unsigned long duty_A,duty_B;
      // Set duty cycle  
  TimerMatchSet(PWM_TIMER_BASE_A, TIMER_A,(PWM_Period_L-PWM_Period_L*dutyCycle_A/1000));
  TimerMatchSet(PWM_TIMER_BASE_B, TIMER_A,(PWM_Period_L-PWM_Period_L*dutyCycle_B/1000));
}

void Motor_L_Freq( unsigned long freq ){
    // Calculate PWM period relative to the system clock
  //PWM_Period_L = SysCtlClockGet() / freq; // Speed in Hz
  PWM_Period_L = CLOCK_L / freq; // Speed in Hz
    // Set the PWM period
  TimerLoadSet(PWM_TIMER_BASE_A,TIMER_A,PWM_Period_L);
  TimerLoadSet(PWM_TIMER_BASE_B,TIMER_A,PWM_Period_L);
}

void Motor_L_Init( void ){
    // Enable Timer
  SysCtlPeripheralEnable(PWM_TIMER_PERIPH_ENABLE_A);
  SysCtlPeripheralEnable(PWM_TIMER_PERIPH_ENABLE_B);
    // Enable Ports
  SysCtlPeripheralEnable(PWM_SYSCTL_PORT_A);
  SysCtlPeripheralEnable(PWM_SYSCTL_PORT_B);
  //TimerConfigure(PWM_TIMER_BASE_A,TIMER_CFG_16_BIT_PAIR|TIMER_CFG_A_PWM);
  //TimerConfigure(PWM_TIMER_BASE_B,TIMER_CFG_16_BIT_PAIR|TIMER_CFG_A_PWM);
  
  Motor_L_Freq(100);
  Motor_L_DutyCycle(0, 600);
  //TIMER0_CTL_R |= 0x40;
  //TIMER1_CTL_R |= 0x40;
  GPIODirModeSet(PWM_PORT_BASE_A,PWM_PORT_PIN_A,GPIO_DIR_MODE_HW);
  GPIODirModeSet(PWM_PORT_BASE_B,PWM_PORT_PIN_B,GPIO_DIR_MODE_HW);  
  GPIOPadConfigSet(PWM_PORT_BASE_A,PWM_PORT_PIN_A,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD);
  GPIOPadConfigSet(PWM_PORT_BASE_B,PWM_PORT_PIN_B,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD);
  GPIOPinConfigure(PWM_PORT_PIN_CONFIG_A);
  GPIOPinConfigure(PWM_PORT_PIN_CONFIG_B);
  //GPIO_PORTD_AFSEL_R |= 0x10;
  GPIOPinTypePWM(PWM_PORT_BASE_A,PWM_PORT_PIN_A);
  GPIOPinTypePWM(PWM_PORT_BASE_B,PWM_PORT_PIN_B);
  TimerEnable(PWM_TIMER_BASE_A, TIMER_A);
  TimerEnable(PWM_TIMER_BASE_B, TIMER_A);
}


